<template>
  <div>
    <van-nav-bar fixed title="对账单" left-arrow class="color-white bg-primary" @click-left="$utils.back($route.path)" />
    <div class="has-top pos-r ">
      <div class="topmain bg-primary">
        <h2 v-if="active==1">{{TotalMoney.ReceivableMoney}}</h2>
        <h2 v-else>{{TotalMoney.PayableMoney}}</h2>
        <span class="tip fs-26 mb-30">应付金额</span>
        <!-- <div class="right pr-20">
          <div class="tbs">
            <div :class="active==1?'active color-primary':''" @click="tab(1)">应收</div>
            <div :class="active==2?'active color-primary':''" @click="tab(2)">应付</div>
          </div>
        </div> -->
         <div class="right pr-20 pt-20 pb-20">
              <div
              class="date-box color-primary dis-inb flex-s-c plr-20 br-5 flex-s-0 flex-g-0"
              @click="startDateSelect()"
            >
              <span>{{ requestData.StartTime }}</span>
              <van-icon :name="Icon" size="12" />
            </div>
            <div class="line"><div></div></div>
            <div
              class="date-box color-primary dis-inb flex-s-c plr-20 br-5 flex-s-0 flex-g-0"
              @click="endDateSelect()"
            >
              <span>{{ requestData.EndTime }}</span>
              <van-icon :name="Icon" size="12" />
            </div>
             <div class="line"><div></div></div>
            <div
              class="date-box color-primary dis-inb flex-s-c plr-20 br-5 flex-s-0 flex-g-0"
              @click="timeSelect=true"
            >
              <span>{{ timedesc }}</span>
              <van-icon :name="Icon" size="12" />
            </div>
        </div>

      </div>
      <div class="plr-20 bg-white">
        <ul class="mt-20">
          <li class="mb-20" v-for="(item,index) in RewardList" :key="index">
            <div class="flex-s-c pt-20"><div class="fs-26 fw-b">{{item.BillCode}}</div><div class="fs-26 color-ddd">{{item.BillTime}}</div></div>
            <div>{{item.BillTypeName}}</div>
            <div class="main mt-10">
              <div>
                <div class="color1 fw-b fs-28">{{(item.AddMoney).toFixed(2)}}</div>
                <div class="fs-22">增加金额(元)</div>
              </div>
              <div>
                <div class="color2 fw-b fs-28">{{(item.LessenMoney).toFixed(2)}}</div>
                <div>减少金额(元)</div>
              </div>
              <div>
                <div class="color3 fw-b fs-28">{{(item.BalanceMoney).toFixed(2)}}</div>
                <div>应付金额(元)</div>
              </div>
            </div>
          </li>

        </ul>
        <no-data v-if="RewardList.length === 0" />
        <p v-if="end === true && RewardList.length !== 0" class="tac fs-24 ptb-40 color-gray-light">无更多数据</p>
        <van-popup v-model="startSelect" position="bottom">
          <van-datetime-picker
            v-model="StartTime"
            item-height="45"
            visible-item-count="4"
            type="date"
            title="请选择时间"
            :min-date="minDate"
            :max-date="maxDate"
            @confirm="onConfirm"
            @cancel="clickOverlay"
          />
        </van-popup>
         <van-popup v-model="endSelect" position="bottom">
          <van-datetime-picker
            v-model="EndTime"
            item-height="45"
            visible-item-count="4"
            type="date"
            title="请选择时间"
            :min-date="minDate"
            :max-date="maxDate"
            @confirm="onConfirm1"
            @cancel="clickOverlay1"
          />
        </van-popup>
      </div>
      <van-popup v-model="timeSelect" position="bottom">
          <van-picker
            show-toolbar
            :columns="columns"
            @cancel="timeSelect=false"
            @confirm="onChange"
          />
      </van-popup>
    </div>
  </div>
</template>

<script>
import { Toast } from 'vant'
import datetimeSelect from 'components/datetimeSelect'
import noData from 'components/noData'
import { GetAccountData,GetAccountReceivableList,GetAccountPayableList } from 'api/order'
import utils from 'utils/utils'
export default {
  components: {
    datetimeSelect,
    noData
  },
  data() {
    return {
      StartTime:'',
      EndTime:'',
      minDate: new Date(1920, 0, 1),
      maxDate: new Date(2120, 11, 1),
      timedesc:'全部',
      columns:['全部','今日', '本周', '本月', '今年'],
      active:1,
      bgImg: require('assets/images/integrate.png'),
      // 时间筛选
      datetime: '',
      timeSelect:false,
      dateSelect: false,
      startSelect: false,
      endSelect: false,
      Icon: 'arrow-down',

      TotalMoney: {},
      requestData: {
        PageIndex: 1,
        PageSize: 10,
        StartTime:'',
        EndTime:'',
      },
      RewardList: [],

      // 滚动状态
      totalAmout: 0,
      scollRequest: true,
      end: false
    }
  },
  watch: {
    // dateSelect(newVal, oldVal) {
    //   if (newVal === true) {
    //     this.Icon = 'arrow-up'
    //   } else {
    //     this.Icon = 'arrow-down'
    //   }
    // }
  },
  created() {
    // let datetime = utils.dateFomat('yyyy', new Date()) + '-' + utils.dateFomat('m', new Date()) + '-' + utils.dateFomat('d', new Date())
    const value = utils.today()
    this.requestData.StartTime = '2018-01-01'
    this.requestData.EndTime = value.endDate ? value.endDate.replace(/\./g, '-') : null
    // 2022-08-01
    this.StartTime =  new Date('2018-01-01')
    this.EndTime =  new Date(this.requestData.EndTime)
    this.GetUserWaitSettleReward()
  },
  mounted() {
    this.$nextTick(function() {
      window.addEventListener('scroll', this.onScroll)
    })
  },
  methods: {
    onChange(picker, value, index) {
      this.timeSelect = false;
      this.timedesc = this.columns[value];
            switch (value) {
        case 0:
          this.requestData.StartTime = '2018-01-01'
          this.requestData.EndTime = utils.today().endDate
          this.StartTime =  new Date('2018-01-01')
          this.EndTime =  new Date(this.requestData.EndTime)
          this.reset()
          break
        case 1:
          this.requestData.StartTime = utils.today().startDate
          this.requestData.EndTime = utils.today().endDate
          this.StartTime =  new Date(this.requestData.StartTime)
          this.EndTime =  new Date(this.requestData.EndTime)
          this.reset()
          break
        case 2:
          this.requestData.StartTime = utils.thisWeek().startDate
          this.requestData.EndTime = utils.thisWeek().endDate
          this.StartTime =  new Date(this.requestData.StartTime)
          this.EndTime =  new Date(this.requestData.EndTime)
          this.reset()
          break
        case 3:
          this.requestData.StartTime = utils.thisMonth().startDate
          this.requestData.EndTime = utils.thisMonth().endDate
          this.StartTime =  new Date(this.requestData.StartTime)
          this.EndTime =  new Date(this.requestData.EndTime)
          this.reset()
          break
          case 4:
          this.requestData.StartTime = utils.thisYear().startDate
          this.requestData.EndTime = utils.thisYear().endDate
          this.StartTime =  new Date(this.requestData.StartTime)
          this.EndTime =  new Date(this.requestData.EndTime)
          this.reset()
          break
      }
    },
    tab(num){
      this.active = num;
      this.requestData.PageIndex = 1
      this.RewardList = []
      this.GetUserWaitSettleReward()
    },
    startDateSelect(){
      this.startSelect = true
    },
    endDateSelect(){
      this.endSelect = true
    },
    openDateSelect(num) {
      this.active=num;
      if(num==2){
      this.dateSelect = true
      }else{
         this.requestData.StartTime = ''
          this.requestData.EndTime = ''
          this.requestData.PageIndex = 1
          this.RewardList = []
          this.GetUserWaitSettleReward()
      }
    },
    reset(){
      this.requestData.PageIndex = 1
      this.RewardList = []
      this.GetUserWaitSettleReward()
    },
    onConfirm(value) {
      console.log(value)
      const _value = utils.dateFomat('yyyy-mm-dd', value) 
      this.requestData.StartTime = _value
      this.requestData.PageIndex = 1
      this.RewardList = []
      this.GetUserWaitSettleReward()
      this.startSelect = false
    },
    clickOverlay() {
      this.startSelect = false
    },
    onConfirm1(value) {
      console.log(value)
      const _value = utils.dateFomat('yyyy-mm-dd', value)
      this.requestData.EndTime = _value
      this.requestData.PageIndex = 1
      this.RewardList = []
      this.GetUserWaitSettleReward()
      this.endSelect = false
    },
    clickOverlay1() {
      this.endSelect = false
    },
    GetUserWaitSettleReward() {
      GetAccountData({startTime:this.requestData.StartTime,endTime:this.requestData.EndTime}).then(res => {
        if (res.State==1) {
          this.TotalMoney = res.Data
        }
      })
      let url;
      if(this.active==1){
        url= '/Order/GetAccountReceivableList'
      }else{
        url= '/Order/GetAccountPayableList'
      }
      GetAccountReceivableList(this.requestData,url).then(res => {
        if (res) {
          // this.TotalMoney = res.Data.TotalMoney
          if (res.Data.length && res.Data.length > 0) {
            res.Data.forEach(tem => {
              this.RewardList.push(tem)
            })
          }
           this.totalAmout = res.TotalAmout
          this.scollRequest = true
        }
      })
    },
    // 滚动事件触发下拉加载
    onScroll() {
      const _this = this
      if (_this.scollRequest === true) {
        if (utils.getScrollHeight() - utils.getClientHeight() - utils.getScrollTop() <= 5) {
          _this.scollRequest = false
          console.log('加载')
          if (_this.requestData.PageIndex < _this.totalAmout) {
            _this.requestData.PageIndex++
            _this.GetUserWaitSettleReward()
          } else {
            console.log('end')
            _this.end = true
          }
        }
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.body{
  background: red;
}
.color-white{
  /deep/.van-nav-bar__title,/deep/.van-icon{
    color:$colorF;
  }
}
/deep/.van-hairline--bottom::after{
  border-bottom: none;
}
.date-box {
  height: 50px;
  line-height: 54px;
  background: #fff;
}
.date-box.bg-primary{
  color: #fff;
}
.bg-box{
  width: 100%;
  height: 100px;
  @include bg-primary;
  top: 0;
  left: 0;
  z-index: 0;
}
.out-box{
  position: relative;
  z-index: 1;
  border-radius: 24px 58px 24px 58px;
  background: $colorF;
  padding: 32px;
  .inner-box{
    border-radius: 58px;
    @include bg-primary;
    color:$colorF;
    font-size: 28px;
    padding: 42px 42px 70px;
  }
}
.content{
  position: relative;
  border-radius: 8px;
  overflow: hidden;
  padding: 20px;
  &:after{
    content:'';
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    @include bg-primary;
    opacity: 0.05;
  }
}
.topmain{
    color: #fff;
  h2{
    font-weight: bold;
    font-size: 60px;
    text-align: center;
    padding: 30px 0 0px 0;
  }
  .tip{
    display: block;
    text-align: center;
    margin-top: -20px;
  }
}
.right{
   display: flex;
  justify-content: flex-end;
.tbs{
  display: flex;
  border: 1px solid #fff;
  border-radius: 5px;
  div{
    height: 50px;
    width: 70px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  .active{
    background: #fff;
  }

}
}
.line{
  padding: 5px;
  display: flex;
  justify-content: center;
  align-items: center;
  div{
    width: 15px;
    background: #fff;
    height: 3px;
  }
}
.color-ddd{
  color: #a8a8a8;

}
.main{
  background:#f3f4f6;
  height: 120px;
  display: flex;
  justify-content: space-around;
  &>div{
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
  }
  .color1{
    color: #53a24c;
  }
    .color2{
    color: #bb1718;
  }
    .color3{
    color: #298eed;
  }
}
</style>
